using UnityEngine;
using System.Collections;

public class ManagerSpawnEnemies : MonoBehaviour
{
	//Tempo para spawnar
	public float timeToSpawn;
	
	//Inimigos para tentar spawnar
	public CharacterBehaviour[] enemiesToSpawn;
	
	//Locais para spawnar
	public Transform[] localSpawn;
	
	private GameObject[] tempEnemies;
	
	private int indexSpawn = 0;
	
	void Start()
	{
		tempEnemies = new GameObject[enemiesToSpawn.Length];
		StartCoroutine(Spawn());
	}
	
	private IEnumerator Spawn()
	{
		yield return new WaitForSeconds(timeToSpawn);
		
		if(!tempEnemies[indexSpawn])
		{
			tempEnemies[indexSpawn] = (GameObject)Instantiate(enemiesToSpawn[indexSpawn].gameObject, localSpawn[indexSpawn].position, Quaternion.identity);
		}
		
		indexSpawn ++;
		
		if(indexSpawn == localSpawn.Length)
		{
			indexSpawn = 0;
		}
		
		StartCoroutine(Spawn());
	}
	
	public void CleanEnemies()
	{
		foreach(GameObject g in tempEnemies)
		{
			if(g)
			{
				Destroy(g);
			}
		}
	}
}
